package com.cqupt.slewing.welcome

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import com.cqupt.slewing.MyViewModel
import com.cqupt.slewing.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class LoginFragment : Fragment() {
    private lateinit var myViewModel: MyViewModel
    private lateinit var editTextUserName: EditText
    private lateinit var editTextPassword: EditText
    private lateinit var checkBoxRemember: CheckBox
    private lateinit var buttonLogin: Button
    private lateinit var textViewRegister: TextView

    private lateinit var editor: SharedPreferences.Editor

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_login, container, false)
        editTextUserName = view.findViewById(R.id.editTextUserName)
        editTextPassword = view.findViewById(R.id.editTextPassword)
        checkBoxRemember = view.findViewById(R.id.checkBoxRemember)
        buttonLogin = view.findViewById(R.id.buttonRegister)
        textViewRegister = view.findViewById(R.id.textViewRegister)
        // 初始化shp
        val shp = requireActivity().getSharedPreferences("MyData", Context.MODE_PRIVATE)
        editor = shp.edit()
        // 初始化界面
        editTextUserName.setText(shp.getString("username",""))
        return view
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        myViewModel = ViewModelProvider(requireActivity())[MyViewModel::class.java]

        buttonLogin.setOnClickListener {
            val username = editTextUserName.text.toString()
            val password = editTextPassword.text.toString()
            val remember = checkBoxRemember.isChecked

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(
                    requireActivity(),
                    "Please enter username or password!",
                    Toast.LENGTH_SHORT
                ).show()
            } else {
                // 本地储存userName
                editor.putString("username", username)
                editor.apply()

                lifecycleScope.launch(Dispatchers.Main) {
                    val session = myViewModel.userLogin(username, password, remember)
                    editor.putString("session", session)
                    editor.apply()

                    if (session.isNotBlank()) {
                        val navController = Navigation.findNavController(it)
                        navController.navigate(R.id.action_loginFragment_to_mainActivity)
                    }else{
                        Toast.makeText(
                            requireActivity(),
                            "Username or password error",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            }
        }
        textViewRegister.setOnClickListener {
            val navController = Navigation.findNavController(it)
            navController.navigate(R.id.action_loginFragment_to_registerFragment)

        }
    }
}